/* 
 * Copyright 2013 Imanuel Rohlfing, Dennis Wittkötter

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
package management.entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;


/**
 * A person object which can be persisted to a database.
 * The person has an unique id, a first name, last name and identity card number.
 * A person can be an admin to manage the room administration.
 * Furthermore the person has a collection of rooms to which it has access.
 * @author Imanuel Rohlfing
 */
@Entity
@ManagedBean(name="person")
@RequestScoped
public class Person implements Serializable {

    private static final long serialVersionUID = 1L;
    
    /**
     * An unique id. It is generated automatically.
     */
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    
    /**
     * The first name of the person.
     */
    private String firstName;
    
    /**
     * The last name of the person.
     */
    private String lastName;
    
    /**
     * The identity card number of the person.
     */
    private String identityCardNumber;
    
    /**
     * A collection of rooms to which the person has access.
     */
    @ManyToMany(mappedBy = "persons")
    private List<Room> rooms;
    
    /**
     * Default constructor to create a person object.
     */
    public Person() {
        this.rooms = new ArrayList<>();
    }

    /**
     * Creates a person object.
     * @param firstName the first name
     * @param lastName  the last name
     * @param identityCardNumber    the identity card number
     */
    public Person(String firstName, String lastName, String identityCardNumber) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.identityCardNumber = identityCardNumber;
        this.rooms = new ArrayList<>();
    }
    
    /**
     * Provides the unique id of the person.
     * @return  person id
     */
    public int getId() {
        return id;
    }
    
    /**
     * Provides the first name.
     * @return  first name
     */
    public String getFirstName() {
        return firstName;
    }

    /**
     * Sets the first name of the person.
     * @param firstName     first name
     */
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    /**
     * Provides the last name of the person.
     * @return  last name
     */
    public String getLastName() {
        return lastName;
    }

    /**
     * Sets the last name of the person.
     * @param lastName  last name
     */
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    
    /**
     * Provides the identity card number of the person.
     * @return  number of identity card
     */
    public String getIdentityCardNumber() {
        return identityCardNumber;
    }

    /**
     * Sets the identity card number of the person.
     * @param identityCardNumber    number of identity card
     */
    public void setIdentityCardNumber(String identityCardNumber) {
        this.identityCardNumber = identityCardNumber;
    }

    /**
     * @return the rooms
     */
    public List<Room> getRooms() {
        return rooms;
    }

    /**
     * @param room the room to set
     */
    public void setRoom(Room room) {
        this.rooms.add(room);
    }
 
    /**
     * @param room the room to remove
     */
    public void removeRoom(Room room) {
        this.rooms.remove(room);
    } 
    
    /**
     * @param rooms the rooms to set
     */
    public void setRooms(List<Room> rooms) {
        this.rooms = rooms;
    }
    
    @Override
    public String toString() {
        return firstName + " " + lastName;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 29 * hash + this.id;
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Person other = (Person) obj;
        if (this.id != other.id) {
            return false;
        }
        return true;
    }
    
    
}
